home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / jovept2.arc / MOVE.C < prev    next >
Text File  |  1985-05-30  |  5KB  |  342 lines

  1. /* move.c */
  2.  
  3. /* JOVE/MSDOS. K. Mitchum 1/85 */
  4. /* Modifications for personal use only. */
  5. /* original code J. Payne LSRHS 5/83 */
  6. /* Ken Mitchum */
  7. /* University of Pittsburgh */
  8. /* Decision Systems Laboratory */
  9.  
  10. /*
  11.    Jonathan Payne at Lincoln-Sudbury Regional High School 5-25-83
  12.   
  13.    jov_move.c
  14.  
  15.    Commands that move the point around in the buffer. */
  16.  
  17. #include "jove.h"
  18.  
  19. #ifdef UNIX
  20. #include <ctype.h>
  21. #endif
  22.  
  23. char    REsent[] = "[?.!]\"  *\\|[.?!]  *\\|[.?!][\"]*$";
  24.  
  25. static int line_pos;
  26.  
  27.  
  28. LINE *
  29. next_line(line, num)
  30. register LINE    *line;
  31. register int    num;
  32. {
  33.     register int    i;
  34.  
  35.     if (line)
  36.         for (i = 0; i < num && line->l_next; i++, line = line->l_next)
  37.             ;
  38.  
  39.     return line;
  40. }
  41.  
  42. LINE *
  43. prev_line(line, num)
  44. register LINE    *line;
  45. register int    num;
  46. {
  47.     register int    i;
  48.  
  49.     if (line)
  50.         for (i = 0; i < num && line->l_prev; i++, line = line->l_prev)
  51.             ;
  52.  
  53.     return line;
  54. }
  55.  
  56. ForChar()
  57. {
  58.     register int    num = exp;
  59.  
  60.     exp = 1;
  61.     while (num--) {
  62.         if (eolp()) {            /* Go to the next line */
  63.             if (curline->l_next == 0)
  64.                 break;
  65.             SetLine(curline->l_next);
  66.         } else
  67.             curchar++;
  68.     }
  69. }
  70.  
  71. BackChar()
  72. {
  73.     register int    num = exp;
  74.  
  75.     exp = 1;
  76.     while (num--) {
  77.         if (bolp()) {
  78.             if (curline->l_prev == 0)
  79.                 break;
  80.             SetLine(curline->l_prev);
  81.             Eol();
  82.         } else
  83.             --curchar;
  84.     }
  85. }
  86.  
  87. NextLine()
  88. {
  89.     if (lastp(curline))
  90.         OutOfBounds();
  91.     down_line(1);
  92. }
  93.  
  94. PrevLine()
  95. {
  96.     if (firstp(curline))
  97.         OutOfBounds();
  98.     down_line(0);
  99. }
  100.  
  101. OutOfBounds()
  102. {
  103.     complain("");
  104. }
  105.  
  106. down_line(down)
  107. {
  108.     LINE    *(*func)() = down ? next_line : prev_line;
  109.     LINE    *line;
  110.  
  111.     line = (*func)(curline, exp);
  112.  
  113.     this_cmd = LINE_CMD;
  114.  
  115.     if (last_cmd != LINE_CMD)
  116.         line_pos = calc_pos(linebuf, curchar);
  117.  
  118.     SetLine(line);        /* Curline is in linebuf now */
  119.     curchar = how_far(curline, line_pos);
  120. }
  121.  
  122. /* Returns what cur_char should be for that pos. */
  123.  
  124. how_far(line, ypos)
  125. LINE    *line;
  126. {
  127.     register char    *pp;
  128.     char    *base;
  129.     register int    cur_char;
  130.     char    c;
  131.     register int    y;
  132.  
  133.     base = pp = getcptr(line, genbuf);
  134.  
  135.     cur_char = 0;
  136.     y = 0;
  137.  
  138.     while (c = *pp++) {
  139.         if (y >= ypos)
  140.             return cur_char;
  141.         if (c == 0177)
  142.             y++;
  143.         else if (c < ' ') {
  144.             if (c == 011)
  145.                 y += ((tabstop - y % tabstop) - 1);
  146.             else
  147.                 y++;
  148.         }
  149.         y++;
  150.         cur_char++;
  151.     }
  152.  
  153.     return pp - base - 1;
  154. }
  155.  
  156. Bol()
  157. {
  158.     curchar = 0;
  159. }
  160.  
  161. Eol()
  162. {
  163.     curchar += strlen(&linebuf[curchar]);
  164. }
  165.  
  166. DotTo(line, col)
  167. LINE    *line;
  168. {
  169.     BUFLOC    bp;
  170.  
  171.     bp.p_line = line;
  172.     bp.p_char = col;
  173.     SetDot(&bp);
  174. }
  175.  
  176. /* If bp->p_line is != current line, then save current line.  Then set dot
  177.    to bp->p_line, and if they weren't equal get that line into linebuf  */
  178.  
  179. SetDot(bp)
  180. register BUFLOC    *bp;
  181. {
  182.     register int    notequal = bp->p_line != curline;
  183.  
  184.     if (notequal)
  185.         lsave();
  186.     curline = bp->p_line;
  187.     curchar = bp->p_char;
  188.     if (notequal)
  189.         getDOT();
  190. }
  191.  
  192. Eof()
  193. {
  194.     SetMark();
  195.     SetLine(curbuf->b_dol);
  196.     Eol();
  197. }
  198.  
  199. Bof()
  200. {
  201.     SetMark();
  202.     SetLine(curbuf->b_zero);
  203. }
  204.  
  205.  
  206.  
  207. Bos()
  208. {
  209.     int    num = exp;
  210.     BUFLOC    *bp,
  211.         save;
  212.  
  213.     exp = 1;
  214.     while (num--) {
  215. onceagain:
  216.         bp = dosearch(REsent, -1, 1);
  217.         DOTsave(&save);
  218.         if (bp == 0) {
  219.             Bof();
  220.             break;
  221.         }
  222.         SetDot(bp);
  223.         to_word(1);
  224.         if (curline == save.p_line && curchar == save.p_char) {
  225.             SetDot(bp);
  226.             goto onceagain;
  227.         }
  228.     }
  229. }
  230.  
  231. Eos()
  232. {
  233.     int    num = exp;
  234.     BUFLOC    *bp;
  235.  
  236.     exp = 1;
  237.     while (num-- && (bp = dosearch(REsent, 1, 1)))
  238.         SetDot(bp);
  239.     if (bp == 0)
  240.         Eof();
  241.     else
  242.         to_word(1);
  243. }
  244.  
  245. length(line)
  246. register LINE    *line;
  247. {
  248.     register char    *base,
  249.             *cp;
  250.  
  251.     base = cp = getcptr(line, genbuf);
  252.  
  253.     while (*cp++)
  254.         ;
  255.     return cp - base - 1;
  256. }
  257.  
  258. isword(c)
  259. register char    c;
  260. {
  261.     return isalpha(c) || isdigit(c);
  262. }
  263.  
  264. to_word(dir)
  265. {
  266.     register char    c;
  267.  
  268.     if (dir > 0) {
  269.         while ((c = linebuf[curchar]) != 0 && !isword(c))
  270.             curchar++;
  271.         if (eolp()) {
  272.             if (curline->l_next == 0)
  273.                 return;
  274.             SetLine(curline->l_next);
  275.             to_word(dir);
  276.             return;
  277.         }
  278.     } else {
  279.         while (!bolp() && (c = linebuf[curchar - 1], !isword(c)))
  280.             --curchar;
  281.         if (bolp()) {
  282.             if (curline->l_prev == 0)
  283.                 return;
  284.             SetLine(curline->l_prev);
  285.             Eol();
  286.             to_word(dir);
  287.         }
  288.     }
  289. }
  290.  
  291. ForWord()
  292. {
  293.     register char    c;
  294.     register int    num = exp;
  295.  
  296.     exp = 1;
  297.     while (--num >= 0) {
  298.         to_word(1);
  299.         while ((c = linebuf[curchar]) != 0 && isword(c))
  300.             curchar++;
  301.         if (eobp())
  302.             break;
  303.     }
  304.     this_cmd = 0;    /* Semi kludge to stop some unfavorable behavior */
  305. }
  306.  
  307. BackWord()
  308. {
  309.     register int    num = exp;
  310.     register char    c;
  311.  
  312.     exp = 1;
  313.     while (--num >= 0) {
  314.         to_word(-1);
  315.         while (!bolp() && (c = linebuf[curchar - 1], isword(c)))
  316.             --curchar;
  317.         if (bobp())
  318.             break;
  319.     }
  320.     this_cmd = 0;
  321. }
  322.  
  323. /* End of window */
  324.  
  325. Eow()
  326. {
  327.     SetLine(next_line(curwind->w_top, SIZE(curwind) - 1 -
  328.             min(SIZE(curwind) - 1, exp - 1)));
  329.     if (!exp_p)
  330.         Eol();
  331. }
  332.  
  333. /* Beginning of window */
  334.  
  335. Bow()
  336. {
  337.     SetLine(next_line(curwind->w_top, min(SIZE(curwind) - 1, exp - 1)));
  338. }
  339.  
  340.  
  341. /* end */
  342.